package com.ruoyi.web.controller.iot;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.LocalDateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.IotDeviceVo;
import com.ruoyi.system.domain.vo.IotVariableValueVo;
import com.ruoyi.system.domain.vo.IotVariableVo;
import com.ruoyi.system.service.IIotTestService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.IotDevice;
import com.ruoyi.system.service.IIotDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 公司名称：未可自动化 <br/>
 * 作者：weikeauto <br/>
 */
@RestController
@RequestMapping("/iot/device")
public class IotDeviceController extends BaseController
{
    @Autowired
    private IIotDeviceService iotDeviceService;

    @Autowired
    private IIotTestService iIotTestService;

    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IotDevice iotDevice)
    {
//        startPage();
        List<IotDeviceVo> list = iotDeviceService.selectIotDeviceList(iotDevice);
//        iIotTestService.testData();
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotDevice iotDevice)
    {
        List<IotDeviceVo> list = iotDeviceService.selectIotDeviceList(iotDevice);
        ExcelUtil<IotDeviceVo> util = new ExcelUtil<IotDeviceVo>(IotDeviceVo.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(iotDeviceService.selectIotDeviceById(id));
    }

    /**
     * 新增设备
     */
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotDevice iotDevice)
    {
        return toAjax(iotDeviceService.insertIotDevice(iotDevice));
    }

    /**
     * 修改设备
     */
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotDevice iotDevice)
    {
        return toAjax(iotDeviceService.updateIotDevice(iotDevice));
    }

    /**
     * 删除设备
     */
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotDeviceService.deleteIotDeviceByIds(ids));
    }

    @GetMapping("/variableValueList")
    public TableDataInfo variableValueList(@RequestParam("deviceTemplateId") Long deviceTemplateId,
                                           @RequestParam("deviceId") Long deviceId,
                                           @RequestParam(name = "variableName", required = false) String variableName,
                                           @RequestParam(name = "variableType", required = false) Integer variableType) {
//        startPage();
        List<IotVariableValueVo> list = iotDeviceService.variableValueList(deviceTemplateId,deviceId,variableName,variableType);
        return getDataTable(list);
    }

    @GetMapping("/variableList")
    public TableDataInfo variableList(@RequestParam("deviceTemplateId") Long deviceTemplateId) {
//        startPage();
        List<IotVariableVo> list = iotDeviceService.variableList(deviceTemplateId);
        return getDataTable(list);
    }

    @GetMapping("/variableValueHisList")
    public TableDataInfo variableValueHisList(@RequestParam(name = "deviceName", required = false) String deviceName,
                                              @RequestParam(name = "variableName", required = false) String variableName,
                                              @RequestParam(name = "variableType", required = false) Integer variableType,
                                              @RequestParam(name = "startDate", required = false) Date startDate,
                                              @RequestParam(name = "endDate", required = false) Date endDate) {
        if(StringUtils.isEmpty(deviceName)){
            return  getDataTable(Lists.newArrayList());
        }
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        if(startDate != null){
            startTime = LocalDateUtils.dateToLocalDate(startDate).atStartOfDay();
        }
        if(endDate != null){
            endTime = LocalDateUtils.dateToLocalDate(startDate).atTime(23, 59, 59);
        }
        startPage();
        List<IotVariableValueVo> list = iotDeviceService.variableValueHisList(deviceName,variableName,variableType,startTime,endTime);
        return getDataTable(list);
    }

}
